<?php
namespace Index\Controller;
use Think\Controller;
use Think\Verify;
class LoginController extends Controller {
    public function indexAction(){

        layout(false);

        if(isset($_SESSION['username'])){
             $this->success('你已经登录',U('index/index/index'));
        }else{
            $this->display();
        }
        
    }

    public function captchaAction($value='')
    {
    	$config =    array(
		    'fontSize'    =>    35,    // 验证码字体大小
		    'length'      =>    4,     // 验证码位数
		);
		$Verify =   new Verify($config);
		$Verify->entry();
    }

    public function checkcaptchaAction($value='')
    {
    	if(IS_AJAX){
    		
    		$code = I('post.captcha','');

    		$verify = new \Think\Verify();
            $verify->reset = false;

    		if($verify->check($code)){
    			echo 1;
    		}else{
    			echo 0;
    		}


    	}
    }

    public function loginAction()
    {
        if(IS_POST){
            $email = I('post.mail','');
            $password = md5(I('post.password',''));

            $sql = "select * from user where email='{$email}' and password='{$password}'";


            $res = M()->query($sql);

            if($res){

                $_SESSION['username']=$res[0]['username'];
                $_SESSION['id']=$res[0]['id'];
                $_SESSION['face']=$res[0]['face'];

                $this->success('登录成功',U('index/index/index'));

            }else{

                $this->error('用户名或者密码不对',U('index/login/index'));

            }

        }
    }


    public function checkemailAction()
    {
       if(IS_AJAX){
        $email = I('post.email','');

        $sql = "select * from user where email = '{$email}'";

        $res = M()->query($sql);

        if(!$res){
            $arr = array(
                'error'=>'0',
                'info'=>'该邮箱不存在，请注册后登录'
                );
            echo json_encode($arr);
        }else{
            $arr = array(
                'error'=>'1',
                'info'=>'可以'
                );
            echo json_encode($arr);
        }


       }
    }

    public function loginoutAction()
    {
        session_unset();
        session_destroy();
        $this->success('退出成功',U('index/login/index'));
    }

    public function qqAction()
    {
        qqlogin();
    }

    public function returnAction()
    {
        $code = I('get.code','');
        if(!empty($code)){
            $openid = getQqOpenID();

            $sql = "select * from user where qq_openid='{$openid}'";

            $row = M()->query($sql);

            if($row){
                $_SESSION['username']=$row[0]['username'];
                $_SESSION['id']=$row[0]['id'];
                $_SESSION['face']=$row[0]['face'];

                $this->success('登录成功',U('index/index/index'));
            }else{
                
                $this->assign('userinfo',getQqUserInfo());
                $this->assign('openid',$openid);
                $this->display('unfind');
            }


            // var_dump($openid);
            // getQqUserInfo();
        }
        // D4856A4CAA10FDC0D547121A583C89ED
        // D4856A4CAA10FDC0D547121A583C89ED

        // var_dump($_REQUEST);
    }

    public function binqqexistAction()
    {
        var_dump($_POST);
    }
}